package com.sgq.vue.controller.resource;

import com.sgq.vue.config.WeatherDaysConstant;
import com.sgq.vue.dto.UserWeatherDto;
import com.sgq.vue.entity.resource.Weather;
import com.sgq.vue.result.Result;
import com.sgq.vue.service.resource.WeatherService;
import com.sgq.vue.utils.system.MySecurityContextHolder;
import com.sgq.vue.vo.query.resource.UserWeatherQueryVo;
import com.sgq.vue.vo.query.resource.UserWeatherVo;
import com.sgq.vue.vo.query.resource.WeatherQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 83778
 * @date: 2023-12-19 16:26
 * @desc:
 */
@RestController
@RequestMapping("/api/resource/weather")
public class WeatherController {

    @Autowired
    private WeatherService weatherService;

    /**
     *  从数据库查询历史城市天气
     * @param weatherQueryVo
     * @return
     */
      @PreAuthorize("hasAuthority('resource:weather:history')")
      @GetMapping("/history")
      public Result history(WeatherQueryVo weatherQueryVo){
          List<Weather> weathers = weatherService.findWeatherHistory(weatherQueryVo);
          return Result.ok(weathers);
      }

    /**
     * 从线上获取最新天气数据
     * @param
     * @return
     */
    @PreAuthorize("hasAuthority('resource:weather:online')")
    @GetMapping("/online")
    public Result online(WeatherQueryVo weatherQueryVo){
        //获取当前用户id
        Long userId =  MySecurityContextHolder.getSysUserId();
        List<Weather> weathers = weatherService.findWeatherOnline(weatherQueryVo,userId);
        String city = weatherQueryVo.getCity();
        if (!ObjectUtils.isEmpty(city)){
            List<Weather> collect = weathers.stream().filter(weather -> weather.getCity().contains(city)).collect(Collectors.toList());
            return Result.ok(collect);
        }
        return Result.ok(weathers);
    }

    @GetMapping("/dynamicOnline")
    public Result dynamicOnline(WeatherQueryVo weatherQueryVo){
        //获取当前用户id
        Long userId =  MySecurityContextHolder.getSysUserId();
        List<Weather> weathers = weatherService.findWeatherOnlineByDynamic(weatherQueryVo,userId, WeatherDaysConstant.SEVEN_DAYS);
        return Result.ok(weathers);
    }

     //用户添加城市天气
     @PostMapping("/saveUserWeatherAssign")
     public Result saveUserWeatherAssign(@RequestBody UserWeatherDto userWeatherDto){
         Long userId = userWeatherDto.getUserId();
         System.out.println("userId = " + userId);
         List<Long> cityList = userWeatherDto.getList();
         List<Long> collect = cityList.stream().filter(item -> item.toString().length() == 9).collect(Collectors.toList());
         if(weatherService.saveUserWeather(userId,collect)){
             return Result.ok().message("城市添加成功");
         }
         return Result.error().message("城市添加失败");
     }

     //用户删除城市天气
     @DeleteMapping("/delete/{userId}")
     public Result delete(@PathVariable Long userId){

         if(weatherService.removeByUserId(userId)){

             return Result.ok().message("城市删除成功");
         }
         return Result.error().message("城市删除失败");
     }

    /**
     * 为用户从角色所拥有的城市中分配城市
     * 分配城市天气-查询城市树数据
     * getUserId 当前用户拥有哪些城市权限 （少）
     * getRoleIds 被操作角色本身拥有哪些城市权限 （多）
     * @param   weatherQueryVo
     * @return
     */
    @GetMapping("/getAssignUserWeatherTree")
    public Result getAssignUserWeatherTree(UserWeatherQueryVo weatherQueryVo) {
        //调用查询权限树数据的方法
        UserWeatherVo userWeatherVo = weatherService.findUserWeatherTree(weatherQueryVo.getUserId(), weatherQueryVo.getRoleIds());
        //返回数据
        return Result.ok(userWeatherVo);
    }



}
